package org.testis.gui.mvc.controller.testdesign;

import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.model.testdesign.PostConditionCaseModel;
import org.testis.gui.mvc.view.testdesign.PostConditionCaseView;
import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.PostConditionCaseRefined;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.orm.testdesign.TestPartSaver;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class PostConditionCaseController extends AbstractController implements SaveAble, Addable {

    public static final String SHOW_ADD_POST_CONDITION_CASE =
            "PostConditionCaseVisibility";
    public static final String CHANGE_NAME = "Name";
    public static final String CHANGE_DESCRIPTION = "Description";
    public static final String CHANGE_TIMEOUT = "TimeOut";
    public static final String CHANGE_CRITICAL = "Critical";
    private TestDesignTreeNode m_selectedNode = null;

    public PostConditionCaseController() {
        super();
    }

    public void operationCloseWindow() {
        this.cleanAll();
    }

    public void operationNewPostConditionCase() {
        initialize(false);
        m_selectedNode = TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode();
        setModelProperty(SHOW_ADD_POST_CONDITION_CASE,
                true);
    }

    public void initialize(boolean editMode) {
        PostConditionCaseModel postConditionCaseModel =
                new PostConditionCaseModel();
        PostConditionCaseView postConditionCaseView =
                new PostConditionCaseView(TestDesignControllerList.newInstance().
                getParentFrame(),
                true,
                editMode);
        super.addModel(postConditionCaseModel);
        super.addView(postConditionCaseView);
        postConditionCaseView.pack();
    }

    public void operationEditPostConditionCase(TestDesignTreeNode node) {
        if (node.getReference().getType() == null) {
            System.out.println("Not found reference type for PostConditionCase");
            return;
        } else if (node.getReference().getType().getName() == null) {
            System.out.println(
                    "Not found reference type name for PostConditionCase");
            return;
        }
        if (node.getReference().getType().getName().equals("PostConditionCase")) {
            initialize(true);
            PostConditionCaseRefined designpart = null;
            try {
                designpart = (PostConditionCaseRefined) node.getObject();
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }
            if (designpart == null) {
                System.out.println("Not found design part for PostConditionCase");
                return;
            }
            setModelProperty(CHANGE_NAME,
                    designpart.getName().toString());
            setModelProperty(CHANGE_DESCRIPTION,
                    designpart.getReference().getDescription().toString());
            Integer timeout = node.getReference().getParameterIntValue(TestDesignTreeParameters.Timeout.name());
            if (timeout != null) {
                setModelProperty(CHANGE_TIMEOUT,
                        timeout);
            }

            Boolean critical = node.getReference().getParameterBooleanValue(TestDesignTreeParameters.Critical.name());
            if (critical != null) {
                setModelProperty(CHANGE_CRITICAL,
                        critical);
            }
            m_selectedNode = node;
            setModelProperty(SHOW_ADD_POST_CONDITION_CASE,
                    true);
        }
    }

    public void operationSavePostConditionCase(String name,
            String description,
            String critical) {
        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("name",
                name);
        params.put("description",
                description);
        params.put("critical",
                critical);
        TestDesignControllerList.newInstance().getTreeController().
                operationHandleSaveClones(this,
                m_selectedNode,
                params);
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
            TreeNode node,
            boolean dynamicSaveOnly) {
        return TestPartSaver.operationSave(params,
                (TestDesignTreeNode) node);
    }

    public void operationAddPostConditionCase(String name,
            String description,
            String critical) throws PersistentException {
        TestDesignTreeNode node = TestDesignControllerList.newInstance().
                getTreeController().getSelectedTreeNode();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("name",
                name);
        params.put("description",
                description);
        params.put("critical",
                critical);
        TestDesignControllerList.newInstance().getTreeController().operationHandleAdd(this, node, params);
    }

    public void operationChangeName(String name) {
        setModelProperty(CHANGE_NAME,
                name);
    }

    public void operationChangeDescription(String description) {
        setModelProperty(CHANGE_DESCRIPTION,
                description);
    }

    public void operationChangeTimeout(String timeout) {
        setModelProperty(CHANGE_TIMEOUT,
                timeout);
    }

    public void operationChangeCritical(String critical) {
        setModelProperty(CHANGE_CRITICAL,
                critical);
    }

    @Override
    public TestDesignTreeNode operationAddObject(HashMap<String, Object> params, TreeNode node, int childIndex) {
        TestDesignTreeNode nodeToAdd = TestPartSaver.operationAdd(params,
                (TestDesignTreeNode) node, childIndex, "PostConditionCase");

        return nodeToAdd;
    }

    @Override
    public boolean isValidSave(HashMap<String, Object> params, TreeNode node) {
        return true;
    }

    @Override
    public boolean isValidAdd(HashMap<String, Object> params, TreeNode node) {
        return true;
    }
}
